using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;

namespace PPIDesktopClient.General
{
    public static class UIHelper
    {
        /// <summary>
        /// Fills a drop down list
        /// </summary>
        /// <param name="dropDown"></param>
        /// <param name="dataSource"></param>
        /// <param name="dataTextField"></param>
        /// <param name="dataValueField"></param>
        /// <param name="additionalItem">Item to add in the first line</param>
        public static void FillDropDown(ComboBox dropDown, DataTable dataTable, string dataTextField, string dataValueField, string additionalItem)
        {
            try
            {
                dropDown.BeginUpdate();
                dropDown.Items.Clear();
                if (!String.IsNullOrEmpty(additionalItem))
                    dropDown.Items.Insert(0, new ListBoxItem(additionalItem, "0"));
                foreach (DataRow _Dr in dataTable.Rows)
                    dropDown.Items.Add(new ListBoxItem(_Dr[dataTextField].ToString(), _Dr[dataValueField].ToString()));
                if (dropDown.Items.Count > 0)
                    dropDown.SelectedIndex = 0;
                dropDown.EndUpdate();
            }
            catch
            {
                throw;
            }
        }
        public static void FillListBox(ListBox listBox, DataTable dataTable, string dataTextField, string dataValueField, string additionalItem)
        {
            try
            {
                listBox.BeginUpdate();
                listBox.Items.Clear();
                if (!String.IsNullOrEmpty(additionalItem))
                    listBox.Items.Insert(0, new ListBoxItem(additionalItem, "0"));
                foreach (DataRow _Dr in dataTable.Rows)
                    listBox.Items.Add(new ListBoxItem(_Dr[dataTextField].ToString(), _Dr[dataValueField].ToString()));
                listBox.SelectedIndices.Clear();
                listBox.EndUpdate();
            }
            catch
            {
                throw;
            }
        }

        public static void FillDropDown(ComboBox dropDown, IList dataSource, Type objectType, string dataTextField, string dataValueField, string additionalItem)
        {
            try
            {
                dropDown.BeginUpdate();

                dropDown.Items.Clear();
                AddItems(dataSource, objectType, dropDown, dataValueField, dataTextField, additionalItem);
                dropDown.DisplayMember = dataTextField;
                dropDown.ValueMember = dataValueField;


                dropDown.EndUpdate();
                if (dropDown.Items.Count > 0)
                    dropDown.SelectedIndex = 0;

            }
            catch
            {
                throw;
            }
        }
        public static void FillListBox(ListBox listBox, IList dataSource, Type objectType, string dataTextField, string dataValueField, string additionalItem)
        {
            try
            {
                listBox.BeginUpdate();

                listBox.Items.Clear();
                AddItems(dataSource, objectType, listBox, dataValueField, dataTextField, additionalItem);
                listBox.DisplayMember = dataTextField;
                listBox.ValueMember = dataValueField;
                listBox.SelectedIndices.Clear();

                listBox.EndUpdate();
            }
            catch
            {
                throw;
            }
        }
		
        /// <summary>
        /// Fills a drop down list
        /// </summary>
        /// <param name="dropDown"></param>
        /// <param name="dataSource"></param>
        /// <param name="dataTextField"></param>
        /// <param name="dataValueField"></param>
        public static void FillDropDown(ComboBox dropDown, DataTable dataTable, string dataTextField, string dataValueField)
        {
            FillDropDown(dropDown, dataTable, dataTextField, dataValueField, null);
        }

        private static void AddItems(IList list, Type type, ComboBox dropDown, string valueMember, string displayMember, string displayText)
        {

            string _Name = "";
            string _Value = "";
            ListBoxItem _Item;
            if (!String.IsNullOrEmpty(displayText))
            {
                dropDown.Items.Add(new ListBoxItem(displayText, "0"));
            }
            for (int i = 0; i < list.Count; i++)
            {
                _Item = new ListBoxItem();
                Object obj = list[i];
                _Name = obj.GetType().GetProperty(displayMember).GetValue(obj, null).ToString();
                _Value = obj.GetType().GetProperty(valueMember).GetValue(obj, null).ToString();

                _Item.Text = _Name;
                _Item.Value = _Value;
                dropDown.Items.Add(_Item);
            }



            ////Creates an instance of the specified type using the constructor that best matches the specified parameters.

            //Object obj = Activator.CreateInstance(type);

            //// Gets the Display Property Information

            //PropertyInfo displayProperty = type.GetProperty(displayMember);

            //// Sets the required text into the display property

            //displayProperty.SetValue(obj, displayText, null);

            //// Gets the Value Property Information

            //PropertyInfo valueProperty = type.GetProperty(valueMember);

            //// Sets the required value into the value property

            //valueProperty.SetValue(obj, -1, null);

            //// Insert the new object on the list

            //list.Insert(0, obj);
        }
        private static void AddItems(IList list, Type type, ListBox listBox, string valueMember, string displayMember, string displayText)
        {

            string _Name = "";
            string _Value = "";
            ListBoxItem _Item;
            if (!String.IsNullOrEmpty(displayText))
            {
                listBox.Items.Add(new ListBoxItem(displayText, "0"));
            }
            for (int i = 0; i < list.Count; i++)
            {
                _Item = new ListBoxItem();
                Object obj = list[i];
                _Name = obj.GetType().GetProperty(displayMember).GetValue(obj, null).ToString();
                _Value = obj.GetType().GetProperty(valueMember).GetValue(obj, null).ToString();

                _Item.Text = _Name;
                _Item.Value = _Value;
                listBox.Items.Add(_Item);
            }



            ////Creates an instance of the specified type using the constructor that best matches the specified parameters.

            //Object obj = Activator.CreateInstance(type);

            //// Gets the Display Property Information

            //PropertyInfo displayProperty = type.GetProperty(displayMember);

            //// Sets the required text into the display property

            //displayProperty.SetValue(obj, displayText, null);

            //// Gets the Value Property Information

            //PropertyInfo valueProperty = type.GetProperty(valueMember);

            //// Sets the required value into the value property

            //valueProperty.SetValue(obj, -1, null);

            //// Insert the new object on the list

            //list.Insert(0, obj);
        }

        public static Int32 GetItemIndex(ComboBox dropdown, string value)
        {
            Int32 _ReturnValue = -1;

            for (Int32 _I = 0; _I < dropdown.Items.Count && _ReturnValue == -1; _I++)
                if (((ListBoxItem)dropdown.Items[_I]).Value == value)
                    _ReturnValue = _I;

            return _ReturnValue;
        }

        public static Int32 GetItemIndex(ListBox listbox, string value)
        {
            Int32 _ReturnValue = -1;

            for (Int32 _I = 0; _I < listbox.Items.Count && _ReturnValue == -1; _I++)
                if (((ListBoxItem)listbox.Items[_I]).Value == value)
                    _ReturnValue = _I;

            return _ReturnValue;
        }

        public static void FillDateDropDown(ComboBox dropdown, int startYear, int endYear, int interval, string displayText)
        {
            dropdown.Items.Clear();
            dropdown.Items.Add(new ListBoxItem(displayText, "0"));
            while (startYear <= endYear)
            {
                dropdown.Items.Add(new ListBoxItem(startYear.ToString()));
                startYear = startYear + interval;
            }
            dropdown.SelectedIndex = 0;
        }

        public static void FillLengthDropDown(ComboBox dropdown, int starValue, int endValue, int interval, string displayText)
        {
            dropdown.Items.Clear();
            dropdown.Items.Add(new ListBoxItem(displayText, "0"));
            while (starValue <= endValue)
            {
                dropdown.Items.Add(new ListBoxItem(starValue.ToString()));
                starValue = starValue + interval;
            }
            dropdown.SelectedIndex = 0;
        }
    }
}
